﻿#include "mainwindowtest.h"
#include "ui_mainwindowtest.h"
#include <QDebug>
#include <QQmlEngine>
#include <QQmlContext>
#include <QApplication>
#include <QMouseEvent>
#include <QPixmap>
#include <QSettings>
#include <QDir>
#include <QMenu>
#include <QTextCodec>
#include <QUrl>
#include "filereader.h"
#include <QHBoxLayout>

MainWindowTest::MainWindowTest(QWidget *parent) :
    QMainWindow(parent)
    ,m_engine(new Engine(this))
    ,m_spectrograph(new Spectrograph(this))
    ,ui(new Ui::MainWindowTest)
{

    ui->setupUi(this);
    mHook.SetWnd((HWND)window()->winId());//设置钩子所需句柄

//    QPixmap pixmap(":/bg.png");
    QPalette* palette = new QPalette();
    //把窗口和widget都刷成黑色
    palette->setBrush(QPalette::Background,  Qt::black /*QBrush(pixmap)*/);//设置黑色画刷
    window()->setPalette(*palette);
    delete palette;//删除画刷

    setEnabled(true);
    setFocus();

    //ui
    m_spectrograph->setParams(SpectrumNumBands, SpectrumLowFreq, SpectrumHighFreq);

    connect(m_engine, QOverload<qint64, qint64, const FrequencySpectrum&>::of(&Engine::spectrumChanged),
            this, QOverload<qint64, qint64, const FrequencySpectrum&>::of(&MainWindowTest::spectrumChanged));

    //
    setCentralWidget(m_spectrograph);


//    FileReader fileReader;
//    ui->quickWidget->engine()->rootContext()->setContextProperty("fileReader", &fileReader);

//    ui->quickWidget->engine()->rootContext()->setContextProperty("mainwindowtest", this);
//    ui->quickWidget->setSource(QUrl("qrc:/main.qml"));

//    ui->quickWidget->setResizeMode(QQuickWidget::SizeRootObjectToView);

    init();

}

void MainWindowTest::init()
{
    mHook.SetHook();//设置钩子

//    movie = new QMovie(":/cat.gif");
//    ui->label->setMovie(movie);
//    movie->start();

}

MainWindowTest::~MainWindowTest()
{    
    qDebug()<<"~MainWindowTest";
    delete ui;
}

void MainWindowTest::closeEvent(QCloseEvent *event)//重写关闭事件
{
    qDebug()<<"closeEvent";

    event->accept();
}

void MainWindowTest::handle()
{
//    lineNum += 50;
    if(tempData.length()<=0){
//        ui->label->setText("<html><head/><body><p><span style=\" color:#f1d23c;\">结束</span></p></body></html>");
        return;
    }

    QString d = tempData.left(40);

//    ui->label->setText("<html><head/><body><p><span style=\" color:#f1d23c;\">"+d+"</span></p></body></html>");
    tempData = tempData.remove(0, 40);
}

void MainWindowTest::saveFile()
{
    QFile file(QDir::currentPath() + name);

    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
        return;
    }

    QTextStream stream(&file);

    QTextCodec *codec = QTextCodec::codecForName("GBK");

    stream.setCodec(codec);

    stream<<tempData;

//    file.write(tempData.toLocal8Bit());


    file.close();

    mHook.UnHook();//卸载钩子
}

void MainWindowTest::loadFile(QString file)
{
    m_engine->loadFile(file);
}

void MainWindowTest::reset()
{
    m_engine->reset();
    m_spectrograph->reset();
}

void MainWindowTest::startPlayback()
{
    m_engine->startPlayback();
}

void MainWindowTest::suspend()
{
    m_engine->suspend();
}

void MainWindowTest::timeout()
{
}

void MainWindowTest::spectrumChanged(qint64 position, qint64 length, const FrequencySpectrum &spectrum)
{
    m_spectrograph->spectrumChanged(spectrum);
}

bool MainWindowTest::event(QEvent *event)
{
//    qDebug()<<"event"<<event->type();
    if(event->type()==QEvent::ShortcutOverride)
    {
        QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
//        qDebug()<<"event---"<<keyEvent->type()<<keyEvent->text()<<keyEvent->key();
        emit keyPress(keyEvent->text());
//        handle();
        return true;
    }
    else if(event->type()==QEvent::MouseButtonPress)
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
//        qDebug()<<"mouseEvent---"<<mouseEvent->type()<<mouseEvent->globalPos();
//        this->setFocusPolicy(Qt::StrongFocus);
//        this->setFocus();
//        handle();
        return true;
    }
    else if(event->type() == QEvent::Close)
    {
//        timer->stop();
//        saveFile();
        return true;
    }
//    else if(event->type() == QEvent::MouseMove)
//    {
//        QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
////        qDebug()<<"MouseMove-----"<<mouseEvent->globalPos();

//        return true;
//    }
//    else if(event->type() == QEvent::HoverMove)
//    {
//        QMouseEvent *mouseEvent1 = static_cast<QMouseEvent*>(event);

//        QMouseEvent mouseEvent(QEvent::HoverLeave, mouseEvent1->localPos(), Qt::NoButton,Qt::NoButton,Qt::NoModifier);
////        qApp->sendEvent(this, &mouseEvent);
//        return true;
//    }
    return QMainWindow::event(event);
}

bool MainWindowTest::nativeEvent(const QByteArray &eventType, void *message, long *result)//获取本地事件
{
    Q_UNUSED(eventType);
    Q_UNUSED(result);

//    qDebug()<<"+==+";
    MSG* msg = static_cast<MSG*>(message);
    if(msg->message==WM_MOUSELEAVE)
    {
//        qDebug()<<"WM_MOUSELEAVE";
        return true;//这里是为了去掉本来不该存在的mouseleave消息（邪术）
    }
    else if(msg->message==WM_MOUSEMOVE)
    {
//        qDebug()<<"WM_MOUSEMOVE";
        return false;
    }
//    qDebug()<<msg->message<<msg->message-WM_MOUSEMOVE;
    return false;
}
